package com.ruoyi.did.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.did.domain.DidDocument;
import com.ruoyi.did.domain.DidMetadata;
import com.ruoyi.did.service.IDidDocumentService;

@RestController
@RequestMapping("/did/document")
public class DidDocumentController extends BaseController {

    @Autowired
    private IDidDocumentService didService;

    @Autowired
    private ObjectMapper objectMapper; // Jackson 用于 JSON 转换

    @PreAuthorize("@ss.hasPermi('did:document:list')")
    @GetMapping("/list")
    public TableDataInfo list(DidDocument didDocument) {
        startPage();
        List<DidDocument> list = didService.selectDidDocumentList(didDocument);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('did:document:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(didService.selectDidDocumentById(id));
    }

    @PreAuthorize("@ss.hasPermi('did:document:add')")
    @Log(title = "DID 文档", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Map<String, Object> requestBody) {
        try {
            // 转换主对象
            DidDocument didDocument = objectMapper.convertValue(requestBody, DidDocument.class);

            // 转换 metadata
            Map<String, Object> metaMap = (Map<String, Object>) requestBody.get("metadata");
            DidMetadata metadata = objectMapper.convertValue(metaMap, DidMetadata.class);

            return toAjax(didService.insertDidDocument(didDocument, metadata));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("DID 创建失败：" + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('did:document:edit')")
    @Log(title = "DID 文档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Map<String, Object> requestBody) {
        try {
            // 转换主对象
            DidDocument didDocument = objectMapper.convertValue(requestBody, DidDocument.class);

            // 转换 metadata
            Map<String, Object> metaMap = (Map<String, Object>) requestBody.get("metadata");
            DidMetadata metadata = objectMapper.convertValue(metaMap, DidMetadata.class);

            return toAjax(didService.updateDidDocument(didDocument, metadata));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("DID 更新失败：" + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('did:document:remove')")
    @Log(title = "DID 文档", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(didService.deleteDidDocumentByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('did:document:revoke')")
    @Log(title = "DID 文档", businessType = BusinessType.UPDATE)
    @PostMapping("/revoke")
    public AjaxResult revoke(@RequestParam String did) {
        boolean ok = didService.revokeDid(did);
        if (!ok) {
            return AjaxResult.error("DID 不存在或已撤销");
        }
        return AjaxResult.success();
    }
}
